package com.comtime.mybox;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.NumberPicker.OnValueChangeListener;

import com.comtime.manager.SwDeviceNew;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartbox.HBApplication;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.MySharedPreferences;
import com.comtime.view.SelectNameModePopupWindow;

public class BatteryManagerActivity extends BaseActivity {

	TextView tv_battery_times, tv_battery_music, tv_battery_health,tv_battery_warnning_duration;
	ToggleButton tb_battery, tb_low_battery_device_vibrate;
	MySharedPreferences mySharedPreferences;
	HBApplication application;

	String[] values;
	
	String[] durationsValues;
	int[] durationsValuesInt;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_battery_manager);
		mySharedPreferences = MySharedPreferences.getInstance(this);
		application = (HBApplication) getApplication();
		init();
	}

	void init() {
		values = new String[] { "1", "3", "5" };

		
		durationsValues= new String[] { "10s", "20s", "30s","40s" };
		durationsValuesInt= new int[] { 10, 20, 30,40 };
		
		tv_battery_times = (TextView) findViewById(R.id.tv_battery_times);

		int ringTimes = mySharedPreferences.getLowBatteryWainTimes();
		switch (ringTimes) {
		case 1:
			buffBatteryWainNum = 0;
			break;
		case 3:
			buffBatteryWainNum = 1;
			break;

		case 5:
			buffBatteryWainNum = 2;
			break;

		default:
			break;
		}
		
		
		int duration=mySharedPreferences.getLowBatteryWarnningDuration();
		
		
		tv_battery_warnning_duration= (TextView) findViewById(R.id.tv_battery_warnning_duration);
		tv_battery_warnning_duration.setText(duration+"s");
		for (int i = 0; i < durationsValues.length; i++) {
			if(durationsValuesInt[i]==duration){
				buffBatteryWainDurationIndex=i;
				break;
			}
		}
		
		
		
		tv_battery_times.setText(ringTimes + "");
		//tv_battery_music = (TextView) findViewById(R.id.tv_battery_music);
		
	

		tv_battery_health = (TextView) findViewById(R.id.tv_battery_health);

		tb_battery = (ToggleButton) findViewById(R.id.tb_low_battery_wain);
		tb_battery.setChecked(mySharedPreferences.getLowBatteryWain());
		tb_battery.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton button, boolean b) {
				// TODO Auto-generated method stub
				mySharedPreferences.saveLowBatteryWain(b);
				if (b) {
					// 开启

					for (SwDeviceNew device : application.swDevicesList) {
						if (device.getBluetoothDevice() != null
								&& device.getBluetoothDevice().getAddress() != null) {
							String address = device.getBluetoothDevice()
									.getAddress().replace(":", "")
									.toUpperCase();
							if (address.equals(mySharedPreferences
									.getBluAdress())) {
								device.sendToDevice(11);
								break;
							}
						}
					}
				} else {
					// 关闭
					for (SwDeviceNew device : application.swDevicesList) {
						if (device.getBluetoothDevice() != null
								&& device.getBluetoothDevice().getAddress() != null) {
							String address = device.getBluetoothDevice()
									.getAddress().replace(":", "")
									.toUpperCase();
							if (address.equals(mySharedPreferences
									.getBluAdress())) {
								device.sendToDevice(10);
								device.musicPlayer.stopPlay();
								break;
							}
						}

					}
					application.swDevicesList.get(0).musicPlayer.stopPlay();

				}
			}
		});

		tb_low_battery_device_vibrate = (ToggleButton) findViewById(R.id.tb_low_battery_device_vibrate);
		tb_low_battery_device_vibrate.setChecked(mySharedPreferences
				.getLowBatteryVibrate());
		tb_low_battery_device_vibrate
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton button,
							boolean b) {
						// TODO Auto-generated method stub
						mySharedPreferences.saveLowBatteryVibrate(b);

					}
				});

	}

	public void BackAction(View v) {
		this.finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}

	public void BatteryWainTimesAction(View v) {

		showSelectNameModePopupWindow(buffBatteryWainNum, values,
				getText(R.string.wain_times).toString());

	}

	public void BatteryWainDurationAction(View v) {

		selectNameModePopupWindow = new SelectNameModePopupWindow(
				BatteryManagerActivity.this, itemsOnClick2, durationValueChangeListener,
				buffBatteryWainDurationIndex, durationsValues, getText(R.string.wain_duration).toString());
		// 显示窗口
		selectNameModePopupWindow.showAtLocation(
				findViewById(R.id.activity_set_battery_manager),
				Gravity.BOTTOM, 0, 0); // 设置layout在PopupWindow中显示的位置
	}
	
	
	
	public void BatteryWainMusicAction(View v) {
		Intent intent = new Intent(this, SelectVoiceActivity.class);
		intent.putExtra("selectType", 1);
		startActivity(intent);
	}

	SelectNameModePopupWindow selectNameModePopupWindow;

	void showSelectNameModePopupWindow(int currentrvalue, String[] values,
			String title) {
		selectNameModePopupWindow = new SelectNameModePopupWindow(
				BatteryManagerActivity.this, itemsOnClick, valueChangeListener,
				currentrvalue, values, title);
		// 显示窗口
		selectNameModePopupWindow.showAtLocation(
				findViewById(R.id.activity_set_battery_manager),
				Gravity.BOTTOM, 0, 0); // 设置layout在PopupWindow中显示的位置
	}
	OnClickListener itemsOnClick2 = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_setting_selectalarmtimes_ok:
				tv_battery_warnning_duration.setText(durationsValues[buffBatteryWainDurationIndex]);
				mySharedPreferences.saveLowBatteryWarnningDuration (durationsValuesInt[buffBatteryWainDurationIndex] );

				selectNameModePopupWindow.dismiss();
				break;
			case R.id.btn_setting_selectalarmtimes_cancel:
				selectNameModePopupWindow.dismiss();
				break;
			 
			default:
				break;
			}
		}
	};
	OnClickListener itemsOnClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_setting_selectalarmtimes_ok:
				tv_battery_times.setText(values[buffBatteryWainNum]);
				mySharedPreferences.saveLowBatteryWainTimes(Integer
						.parseInt(values[buffBatteryWainNum]));

				selectNameModePopupWindow.dismiss();
				break;
			case R.id.btn_setting_selectalarmtimes_cancel:
				selectNameModePopupWindow.dismiss();
				break;
			 
			default:
				break;
			}
		}
	};

	int buffBatteryWainNum = 1;
	OnValueChangeListener valueChangeListener = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			// TODO Auto-generated method stub
			if (picker.getId() == R.id.numberPicker_setting_times) {
				buffBatteryWainNum = newVal;

			}
		}
	};
	
	int buffBatteryWainDurationIndex=0;
	
	OnValueChangeListener durationValueChangeListener = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			// TODO Auto-generated method stub
			if (picker.getId() == R.id.numberPicker_setting_times) {
				buffBatteryWainDurationIndex = newVal;

			}
		}
	};
	
	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		/*tv_battery_music.setText(mySharedPreferences
				.getLowBatteryWainVoiceTitle());*/
	}

}
